﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.Common;
using System.Linq.Expressions;

namespace CH.Easy.DbProvider.Core
{
    public class DbQueryEnumerable<T> : IEnumerable<T>, IEnumerable
    {
        private DbDataReader reader;
        private Delegate de;
        //private DbQueryEnumerator<T> enumerator;
        public DbQueryEnumerable(DbDataReader reader,LambdaExpression resultExpression) 
        {
            this.reader = reader;
            this.de = resultExpression.Compile();
            //this.enumerator = new DbQueryEnumerator<T>(reader, resultExpression);
        }

        public IEnumerator<T> GetEnumerator()
        {

            //return this.enumerator;
            while (this.reader.Read())
            {
                yield return (T)de.DynamicInvoke(this.reader);
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
    }
}
